package cn.kxhp.mybatis3.dao.sql;

import cn.kxhp.mybatis3.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

/**
 * @author Lianxw
 * @since 2022/3/24
 */
public class UserSqlProvider {

    public static String selectUserWithRole(@Param("condition") final User condition) {
        SQL sql = new SQL()
                .SELECT("u.id as u_id", " u.uid as u_uid", "u.username as u_username",
                        "u.phone as u_phone", "u.`status` as u_status",
                        "r.id as r_id", "r.`code`  as r_code", "r.title  as r_title",
                        "r.status as r_status", "r.`app_id` as r_app_id")
                .FROM("infra_user u")
                .INNER_JOIN("infra_user_role ur on u.id = ur.user_id")
                .INNER_JOIN("infra_role r ON ur.role_id = r.id");
        if (condition != null) {
            if (StringUtils.hasText(condition.getUsername())) {
                sql.WHERE("u.username=#{condition.username}");
            }
        }
        return sql.toString();
    }

}
